//
//  FGNoticeView.m
//  FGChatKit
//
//  Created by Henry on 2021/9/26.
//

#import "FGNoticeView.h"
#import "FGChatConfiguration.h"
#import "FGChatHelper.h"
#import "UIColor+FGChat.h"

@interface FGNoticeView ()

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIView <FGNoticeContentProtocol> *contentView;

@end

@implementation FGNoticeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor fg_colorWithString:@"#757B85"];
        
        [self initView];

    }
    return self;
}

- (void)initView {
    _minHeight = 50;
    _maxHeight = 160;
    
    _dismissDuration = 2;
    
    [self addSubview:self.titleLabel];

}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(10, 0, self.frame.size.width - 20, self.minHeight);
}

#pragma mark - Public

- (void)resetSize:(CGSize)size {
    CGRect rect = self.frame;
    rect.size.width = size.width;
    self.frame = rect;
    if (self.contentView && self.contentView.hidden == NO) {
        self.contentView.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), self.frame.size.width, self.maxHeight - CGRectGetMaxY(self.titleLabel.frame));
    }
}

- (void)show:(UIView *)view title:(NSString *)title {
    [self show:view title:title type:FGNoticeTypeNone];
}

- (void)show:(UIView *)view title:(NSString *)title type:(FGNoticeType)type {
    [self show:view title:title type:type contentView:nil];
}

- (void)show:(UIView *)view title:(NSString *)title type:(FGNoticeType)type contentView:(UIView<FGNoticeContentProtocol> *)contentView {
    self.title = title;
    
    if (!self.hidden && type == self.type) {
        return;
    }
    
    self.type = type;
    
    self.hidden = NO;
    self.frame = CGRectMake(0, [FGChatHelper statusNavHeight], view.frame.size.width, self.minHeight);
    [view addSubview:self];

    if (type == FGNoticeTypeAutoDismiss) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_dismissDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismiss];
        });
    }
    
    if (type == FGNoticeTypeOpen) {
        self.open = NO;
    }
    
    self.contentView = contentView;
    
    self.maxHeight = self.minHeight + [contentView contentHeight];
}

- (void)dismiss {
    if (self.hidden) {
        return;
    }
    
    self.hidden = YES;

    [self.contentView removeFromSuperview];
    self.contentView = nil;
    
    [self.delegate noticeView:self updateHeight:0];
    
}

- (void)onOpen {
    if (!self.contentView) {
        return;
    }
    [self addSubview:self.contentView];
    self.contentView.hidden = NO;
    self.contentView.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), self.frame.size.width, self.maxHeight - CGRectGetMaxY(self.titleLabel.frame));
}

- (void)onClose {
    if (!self.contentView) {
        return;
    }
    self.contentView.hidden = YES;
}

#pragma mark - Event

- (void)onClickTitle {
    if (self.type == FGNoticeTypeAutoDismiss) {
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(noticeView:clickTitle:)]) {
        [self.delegate noticeView:self clickTitle:self.titleLabel.text];
    }
    
    if (self.type == FGNoticeTypeButton) {
        return;
    }
    
    self.open = !self.open;
}

#pragma mark - Setters

- (void)setOpen:(BOOL)open {
    _open = open;
    if (open) {
        [self onOpen];
        [UIView animateWithDuration:0.2 animations:^{
            CGRect rect = self.frame;
            rect.size.height = self.maxHeight;
            self.frame = rect;
        } completion:^(BOOL finished) {
            
        }];
        [self.delegate noticeView:self updateHeight:self.maxHeight];
        
    } else {
        [UIView animateWithDuration:0.2 animations:^{
            CGRect rect = self.frame;
            rect.size.height = self.minHeight;
            self.frame = rect;
        } completion:^(BOOL finished) {
            [self onClose];
        }];
        [self.delegate noticeView:self updateHeight:self.minHeight];

    }
}

- (void)setTitleAlignment:(NSTextAlignment)titleAlignment {
    _titleAlignment = titleAlignment;
    self.titleLabel.textAlignment = titleAlignment;
}

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
}

#pragma mark - Getters

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.font = [FGChatConfiguration shareInstance].defaultFont;
        _titleLabel.numberOfLines = 2;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickTitle)];
        _titleLabel.userInteractionEnabled = YES;
        [_titleLabel addGestureRecognizer:tap];
    }
    return _titleLabel;
}

@end
